package com.yizhuoyan.his;

import com.yizhuoyan.his.webapi.interceptor.SessionInterceptor;
import org.hibernate.validator.HibernateValidator;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.validation.Validation;
import javax.validation.Validator;

@SpringBootApplication
@MapperScan("com.yizhuoyan.his.dao")
@ServletComponentScan
public class HisBackendWebApiApplication implements WebMvcConfigurer {

//    @Override
//    public void addInterceptors(InterceptorRegistry registry) {
//        registry.addInterceptor(new SessionInterceptor())
//                .addPathPatterns("/api/**")
//                .excludePathPatterns("/api/session");
//    }
    /**
     * 验证器
     *
     * @return
     */
    @Bean
    public Validator createValidator() {
        return Validation.byProvider(HibernateValidator.class)
                .configure()
                .allowOverridingMethodAlterParameterConstraint(true)
                .failFast(true)
                .buildValidatorFactory()
                .getValidator();
    }
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/api/**")
                .allowCredentials(true)
                .allowedMethods("*")
                .allowedOrigins("*")
                .maxAge(10000);
    }

    public static void main(String[] args) {
        SpringApplication.run(HisBackendWebApiApplication.class, args);
    }

}
